﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.ComponentModel;
using System.IO;

namespace SlaktDataReader
{
    [Serializable]
    public class MarriageBook : BookBase<MarriageBookRecord>, SlaktDataReader.IBooks
    {
        public override ObservableCollection<MarriageBookRecord> Items { get; set; }
        public override BookTypes BookType { get { return BookTypes.Marriages; } }

        public MarriageBook(string FileName)
        {
            this.FileName = FileName;
            _init();
            ReadBook();
        }

        public MarriageBook()
        {
            _init();
        }

        //private void _init()
        //{
        //    bwLoad = new BackgroundWorker();
        //    bwLoad.DoWork += new DoWorkEventHandler(bwLoad_DoWork);
        //    bwLoad.ProgressChanged += new ProgressChangedEventHandler(bwLoad_ProgressChanged);
        //    bwLoad.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoad_RunWorkerCompleted);
        //}

        public bool Save(string SaveFilename)
        {
            return false;
        }

        protected override void bwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            MarriageBook rv;
            BWArgs arg = e.Argument as BWArgs;

            switch (arg.FileType)
            {
                case BWArgs.BookFileType.Text:
                    rv = _ReadTextFile(arg.FileName);
                    e.Result = rv;
                    break;
                case BWArgs.BookFileType.Excel:
                    rv = _ReadExcelFile(arg.FileName);
                    e.Result = rv;
                    break;
                default:
                    break;
            }


        }

        protected override void bwLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (e.Result != null)
            {
                MarriageBook rv = e.Result as MarriageBook;
                if (rv.Items != null)
                {
                    //this.Area = rv.Area;
                    //this.EditedBy = rv.EditedBy;
                    //this.Parish = rv.Parish;
                    //this.Period = rv.SDCode;
                    //this.Source = rv.Source;
                    //this.UpdateDate = rv.UpdateDate;

                    this.Items = rv.Items;
                    readSuccess();

                    return;
                }
            }
            readFail();
        }

        protected override void bwLoad_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }

        private MarriageBook _ReadTextFile(string fName)
        {
            MarriageBookTextFile reader = new MarriageBookTextFile();
            MarriageBook bBook = new MarriageBook();
            if (reader.Read(fName) > 0)
            {
                bBook.Items = new ObservableCollection<MarriageBookRecord>(reader.Records);
                bBook.FileName = fName;
                // Copy other data
                return bBook;
            }
            return null;
        }

        private MarriageBook _ReadExcelFile(string fName)
        {
            return null;
        }

        public override void ExportBook()
        {
            exportFail();
        }
    }

    [Serializable]
    public class MarriageBookRecord
    {
        private int rowID = 0;
        public int RowID { get { return rowID; } set { rowID = value; IsChanged = true; } }

        private string pageNo = null;
        public string PageNo { get { return pageNo; } set { pageNo = value; IsChanged = true; } }

        private string ordNo = null;
        public string OrdNo { get { return ordNo; } set { ordNo = value; IsChanged = true; } }

        private string banns = null;
        public string Banns { get { return banns; } set { banns = value; IsChanged = true; } }

        private string marriageDate = null;
        public string MarriageDate { get { return marriageDate; } set { marriageDate = value; IsChanged = true; } }

        private string maleTitle = null;
        public string MaleTitle { get { return maleTitle; } set { maleTitle = value; IsChanged = true; } }

        private string maleFirstName = null;
        public string MaleFirstName { get { return maleFirstName; } set { maleFirstName = value; IsChanged = true; } }

        private string firstName = null;
        public string FirstName { get { return firstName; } set { firstName = value; IsChanged = true; } }

        private string maleSurName = null;
        public string MaleSurName { get { return maleSurName; } set { maleSurName = value; IsChanged = true; } }

        private string maleAddress = null;
        public string MaleAddress { get { return maleAddress; } set { maleAddress = value; IsChanged = true; } }

        private string nmAddress = null;
        public string NewMaleAddress { get { return nmAddress; } set { nmAddress = value; IsChanged = true; } }

        private string mParents = null;
        public string MaleParents { get { return mParents; } set { mParents = value; IsChanged = true; } }

        private string mAge = null;
        public string MaleAge { get { return mAge; } set { mAge = value; IsChanged = true; } }


        private string fAge = null;
        public string FemaleAge { get { return fAge; } set { fAge = value; IsChanged = true; } }

        private string fTitle = null;
        public string FemaleTitle { get { return fTitle; } set { fTitle = value; IsChanged = true; } }

        private string ffName = null;
        public string FemaleFirstName { get { return ffName; } set { ffName = value; IsChanged = true; } }

        private string fsName = "";
        public string FemaleSurName { get { return fsName; } set { fsName = value; IsChanged = true; } }

        private string fAddress = null;
        public string FemaleAddress { get { return fAddress; } set { fAddress = value; IsChanged = true; } }

        private string nfAddress = null;
        public string NewFemaleAddress { get { return nfAddress; } set { nfAddress = value; IsChanged = true; } }

        private string fParents = null;
        public string FemaleParents { get { return fParents; } set { fParents = value; IsChanged = true; } }

        private string extra1 = null;
        public string Extra1 { get { return extra1; } set { extra1 = value; IsChanged = true; } }

        private string extra2 = null;
        public string Extra2 { get { return extra2; } set { extra2 = value; IsChanged = true; } }

        private string extra3 = null;
        public string Extra3 { get { return extra3; } set { extra3 = value; IsChanged = true; } }

        private string uppdaterat = "";
        public string Updated { get { return uppdaterat; } set { uppdaterat = value; IsChanged = true; } }

        [XmlIgnore]
        public bool IsChanged { get; set; }
    }
}
